package com.biz.primus.ms.order.service.bulider;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.model.oms.stream.OrderCollectionChannel;
import com.biz.primus.model.order.exception.OrderException;
import com.biz.primus.model.order.vo.request.PaidOrderRequestVO;
import com.biz.primus.ms.order.convertor.OmsOrderConvertor;
import com.biz.primus.ms.order.dao.po.Order;
import com.biz.primus.ms.order.dao.po.OrderSummary;
import com.biz.primus.ms.order.service.helper.OrderDataStreamHelper;
import com.biz.primus.ms.order.service.helper.OrderServiceHelper;
import org.codelogger.utils.BeanUtils;

import java.sql.Timestamp;

/**
 * 已支付订单构建器<br>
 * 修改订单状态，推送OMS
 * @author Tiger Feng
 * @date 2017年11月08日
 */
public class PaidOrderBuilder {
    /**
     * 订单支付回调请求模型
     */
    private PaidOrderRequestVO paidOrderRequestVO;
    /**
     * 订单服务助手，构造方法注入
     */
    private OrderServiceHelper serviceHelper;
    private OrderDataStreamHelper dataStreamHelper;

    private OrderSummary orderSummary;

    private PaidOrderBuilder() {
    }

    private PaidOrderBuilder(PaidOrderRequestVO paidOrderRequestVO,
                             OrderServiceHelper serviceHelper,
                             OrderDataStreamHelper dataStreamHelper) {
        this.paidOrderRequestVO = paidOrderRequestVO;
        this.serviceHelper = serviceHelper;
        this.dataStreamHelper = dataStreamHelper;
    }

    /**
     * 1、创建构建器
     *
     * @param paidOrderRequestVO
     * @param serviceHelper
     * @return
     */
    public static PaidOrderBuilder newBuilder(PaidOrderRequestVO paidOrderRequestVO,
                                              OrderServiceHelper serviceHelper,
                                              OrderDataStreamHelper dataStreamHelper) {
        AssertUtils.notNull(paidOrderRequestVO, OrderException.ORDER_CODE_CANNOT_NULL);
        AssertUtils.notNull(serviceHelper, OrderException.ORDER_HELPER_CAN_NOT_NULL);
        return new PaidOrderBuilder(paidOrderRequestVO, serviceHelper,dataStreamHelper);
    }

    /**
     * 2、更改订单状态
     */
    public PaidOrderBuilder updateLocalOrderState() {
        this.orderSummary = serviceHelper.updateOrderSummary2Paid(paidOrderRequestVO);
        return this;
    }
    /**
     * 3、推送中台
      */
    public PaidOrderBuilder sendOMS(){
        Order store = orderSummary.getStoreOrder();
        if(store != null){
            sendNewOrder2OMS(store);
            // 设置推送中台时间
            store.setSend2OMSTimestamp(new Timestamp(System.currentTimeMillis()));
            serviceHelper.saveOrder(store);
        }
        Order flagship = orderSummary.getFlagshipOrder();
        if(flagship != null){
            sendNewOrder2OMS(flagship);
            // 设置推送中台时间
            flagship.setSend2OMSTimestamp(new Timestamp(System.currentTimeMillis()));
            serviceHelper.saveOrder(store);
        }
        return this;
    }

    public OrderSummary build(){
        return orderSummary;
    }

    private void sendNewOrder2OMS(Order order){
        OrderCollectionChannel.OrderData orderData = new OrderCollectionChannel.OrderData();
        BeanUtils.copyProperties(OmsOrderConvertor.newPaidPo2OmsOrderVo(order),orderData);
        dataStreamHelper.sendNewOrder(orderData);
    }
}
